package com.soft.line.service.impl;

import com.github.pagehelper.PageInfo;
import com.soft.line.entity.LasUserInfo;
import com.soft.line.entity.UserConDep;
import com.soft.line.filter.HttpRequestUtil;
import com.soft.line.filter.UserTokenDto;
import com.soft.line.mapper.UserConDepMapper;
import com.soft.line.service.UserConDepService;
import com.soft.line.util.pagehelper.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserConDepServiceImpl implements UserConDepService {

    @Autowired
    public UserConDepMapper userConDepMapper;

    @Override
    public PageInfo<List<Map<String, Object>>> selectUserConDep(Map<String, Object> map) {
        PageUtil.setPageParam(map);
        List<Map<String, Object>> reultList = new ArrayList<Map<String, Object>>();
        for (LasUserInfo userInfo:userConDepMapper.selectAllUser((String)map.get("userName"))){
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("userName",userInfo.getUserName());
            resultMap.put("userId",userInfo.getId());
            List<String> userConDeps=userConDepMapper.selectAllConDep(userInfo.getId());
            resultMap.put("depList",userConDeps);
            reultList.add(resultMap);
        }
        PageInfo<List<Map<String, Object>>> resultPage = new PageInfo(reultList);
        return resultPage;
    }

    @Override
    public void delete(Map<String, Object> map) {
        userConDepMapper.deleteByUserId((Integer)map.get("userId"));
    }

    @Override
    public void save (Map<String, Object> map) {
        String modifyPerson = "admin";
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            modifyPerson=userToken.getUserInfo().getUserName();
        }
        userConDepMapper.deleteByUserId((Integer)map.get("userId"));
        for (String contractDepartmentId:(List<String>)map.get("depList")){
            userConDepMapper.insert((Integer) map.get("userId"),contractDepartmentId,modifyPerson);
        }
    }
}
